#!/bin/bash

. ./util.sh

CMD=""
SLEEP_NUM=0
FREQ=99
NAME="scylla"
PID=""
PROC_TIMEOUT=1000
RECORD_FILE="perf.data"
SVG_FILE="hikv.svg"
DOWNLOAD_DIR="/tmp/FlameGraph"
LOG_FILE=a.log

usage() {
    echo $0
    echo "Example: "
    echo -e "Just perform perf record:"
    echo -e "\t $0 record --name scylla"
    echo -e "Just perform perf script and flamegraph scripts to generate svg file"
    echo -e "\t $0 svg --record-file perf.data --svg-file hikv.svg"
    echo -e "Do record and generate flamegraph svg file"
    echo -e "\t $0 go --name scylla"
}

check_file_size() {
    touch $LOG_FILE
    while true; do
        sleep 3;
        ls -lh $RECORD_FILE;
        flag=`cat $LOG_FILE`
        if [ x$flag = "x1" ]; then
            echo 0 > $LOG_FILE
            break
        fi
    done
}

perf_record() {
    echo_with_color "perf record(you may need use ctrl+c to stop, and DONOT run too long, or the file will be extremely big) ..."
    # start a sepratate process to monitor RECORD_FILE data size
    check_file_size &
    echo "background process $! for checking file size"
    if perf record -h 2>&1 | grep proc-map-timeout > /dev/null; then
        perf record -F $FREQ -ag --proc-map-timeout $PROC_TIMEOUT --call-graph dwarf -p $PID -o $RECORD_FILE -- sleep $SLEEP_NUM
    else
        perf record -F $FREQ -ag --call-graph dwarf -p $PID -o $RECORD_FILE -- sleep $SLEEP_NUM
    fi
    echo 1 > $LOG_FILE
    echo "Now you may want to use 'svg' command to generate flamegraph"
}

perf_script() {
    echo_with_color "perf script and convert into svg ..."
    [ -e $RECORD_FILE ] || exit_with_message "$RECORD_FILE not existed"
    perf script -i $RECORD_FILE | ./stackcollapse-perf.pl | ./flamegraph.pl > $SVG_FILE
    echo_with_color "$SVG_FILE generated, you can access it in web broswer"
}

do_all() {
    perf_record
    perf_script
}

if [ $# = 0 ]; then
    usage
    exit 0
fi

#for i in "$@"; do echo "param=$i";done

CMD=$1
shift

if ! which git > /dev/null; then yum install -y git; fi
[ -e /tmp/FlameGraph ] ||
    git clone https://github.com/brendangregg/FlameGraph $DOWNLOAD_DIR

cd $DOWNLOAD_DIR

while [ $# -gt 0 ]; do
    case "$1" in
        --name)
            NAME=$2
            PID=`pidof $NAME | cut -d ' ' -f1`; shift 2
            ;;
        --pid)
            PID=$2; shift 2
            ;;
        --record-file)
            RECORD_FILE=$2; shift 2
            ;;
        --svg-file)
            SVG_FILE=$2; shift 2
            ;;
        --sleep-num)
            SLEEP_NUM=$2; shift 2
            ;;
        --proc-timeout)
            PROC_TIMEOUT=$2; shift 2
            ;;
        --sleep)
            SLEEP_NUM=$2; shift 2
            ;;
        *)
            exit_with_message "unsupported option"
            ;;
    esac
    if [ x$NAME = x ] && [ x$PID = x ]; then exit_with_message "name or pid not given"; fi
    if [ x$NAME != x ] && ! pidof $NAME > /dev/null; then exit_with_message "$NAME not started"; fi
done

if [ $CMD = "record" ]; then
    perf_record
elif [ $CMD = "svg" ]; then
    perf_script
elif [ $CMD = "go" ]; then
    do_all
else
    exit_with_message "invalid command"
fi
